package com.rtp2p.rtcoresdkdemo.play

import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.rtp2p.rtcoresdkdemo.R
import com.rtp2p.rtcoresdkdemo.base.BaseActivity
import com.rtp2p.rtcoresdkdemo.databinding.LivePlayActivityBinding
import com.rtp2p.rtp2pcore.utils.RTLogUtils

class LivePlayActivity : BaseActivity() {
    private val viewModel: LiveViewModel by viewModels()
    override fun initWorkspaceAction(savedInstanceState: Bundle?) {
        val binding = DataBindingUtil.setContentView<LivePlayActivityBinding>(
            this,
            R.layout.live_play_activity
        )
        setContentView(binding.root)
        setOnApplyWindowInsetsListener(binding.root)

        val deviceId = intent.getStringExtra("deviceId")
        if (deviceId.isNullOrEmpty()) {
            finish()
            return
        }
        val isAp = intent.getBooleanExtra("isAp",false)
        viewModel.init(this,deviceId,isAp, binding.rtVideoView)
        binding.lifecycleOwner = this
        binding.vm = viewModel

        binding.btnReturn.setOnClickListener { finish() }
        binding.btnReturn2.setOnClickListener {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
        }
        binding.btnQuanping.setOnClickListener {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }
        viewModel.fullscreen.observe(this) {
            RTLogUtils.d("屏幕方向 = ${it}")
            if (it) {
                //横屏
                hideStatusBar(binding.root)

                val layoutParams = binding.llVideoView.layoutParams
                layoutParams.height = Resources.getSystem().displayMetrics.heightPixels
                binding.llVideoView.layoutParams = layoutParams
                // 应用新的参数
                binding.llVideoView.requestLayout()
            } else {
                //竖屏
                showStatusBar(binding.root)

                val layoutParams = binding.llVideoView.layoutParams
                layoutParams.height =
                    Resources.getSystem().displayMetrics.widthPixels * 9 / 16
                binding.llVideoView.layoutParams = layoutParams
                // 应用新的参数
                binding.llVideoView.requestLayout()
            }
        }
    }

    override fun onStart() {
        super.onStart()
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED

        viewModel.onStart()
    }

    override fun onStop() {
        super.onStop()
        viewModel.onStop()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // 横屏
            viewModel.fullscreen.value = true
        } else { // 竖屏
            viewModel.fullscreen.value = false
        }
    }
}